﻿using System;
using System.Dynamic;
using Core.CSV;
using Core.Utils;
using Game.View;
using Game.Weapons;
using Unity.VisualScripting;
using UnityEngine;
using Gun = Game.Weapons.Gun;

namespace Game
{
    public class WeaponFactory
    {
        public static Weapon New(uint id)
        {
            TBWeaponsConfig config = Tables.TBWeapons.GetRow<TBWeaponsConfig>(id);
            return New(config);
        }

        public static Weapon New(TBWeaponsConfig config) {
            Weapon weapon;
            switch (config.ID) {
                /*近战武器*/
                case WeaponID.WOODEN_SWORD:
                    weapon = new WoodenSword(config);
                    break;
                case WeaponID.WOODEN_BOW:
                    weapon = new Gun(config);
                    break;
                default:
                    weapon = new Weapon(config);
                    break;
            }

            weapon.Init();
            return weapon;
        }
        
        public static WeaponDisplay SpawnView(Weapon weapon,Transform parent = null, Action<WeaponDisplay> complete = null) {
            GameObject obj = PoolMgr.Ins.Spawn(weapon.Data.PrefabPath);
            WeaponDisplay display;
            switch (weapon.Data.ID) {
                case WeaponID.WOODEN_SWORD:
                    display = obj.AddComponent<WeaponDisplay>();
                    break;
                default:
                    display =  obj.AddComponent<WeaponDisplay>();
                    break;
            }
            display.gameObject.name = weapon.Name;
            if (parent != null) {
                display.transform.parent = parent;
            }
            display.Init(weapon);
            complete?.Invoke(display);
            return display;
        }
    }
}